package com.niodata.dp.monitor.sharelib.service;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.niodata.dp.monitor.sharelib.entity.task.TaskExecResult;
import com.niodata.dp.monitor.sharelib.entity.task.TaskMonitorResult;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * Created by ndp on 17-7-14.
 */
@Component
public class TaskMonitorService {

  @Autowired
  @Qualifier("monitorRestTemplate")
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  public TaskMonitorResult getTaskAlertMsgs(long userId, List<Long> groupIds, int pageIndex,
        int pageSize, String status, String taskId) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String alertUrl = String.format(
          "http://%s:%s/monitor/task/alerts?pageIndex=%d&pageSize=%d&userId=%d&groupIds=%s",
          server, port, pageIndex, pageSize, userId, groupIdStr);
    if (!Strings.isNullOrEmpty(status)) {
      alertUrl = String.format("%s&status=%s", alertUrl, status);
    }
    if (!Strings.isNullOrEmpty(taskId)) {
      alertUrl = String.format("%s&taskId=%s", alertUrl, taskId);
    }
    return restTemplate
          .getForEntity(alertUrl, TaskMonitorResult.class)
          .getBody();

  }

  public TaskExecResult getTaskExecutions(String taskId, long userId, List<Long> groupIds,
        int pageIndex,
        int pageSize, String status,
        String startTime, String endTime) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/task/execs?pageIndex=%d&pageSize=%d&taskId=%s&userId=%d&groupIds=%s&status=%s&startTime=%s&endTime=%s",
          server, port, pageIndex, pageSize, taskId, userId, groupIdStr, status, startTime,
          endTime);
    return restTemplate
          .getForEntity(execUrl, TaskExecResult.class)
          .getBody();

  }

  public TaskExecResult getRecentFinishedExecutions(long userId, List<Long> groupIds) {
    String groupIdStr = String.join(",", Lists.transform(groupIds, String::valueOf));

    String execUrl = String.format(
          "http://%s:%s/monitor/task/recentfinish?userId=%d&groupIds=%s",
          server, port, userId, groupIdStr);
    return restTemplate
          .getForEntity(execUrl, TaskExecResult.class)
          .getBody();

  }

  public void syncExecutions() {
    String url = String.format(
      "http://%s:%s/monitor/task/syncExecutions", server, port);
    restTemplate.postForEntity(url, null, String.class);
  }

}
